package br.edu.ufcg.splab.parser;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

import br.edu.ufcg.splab.core.InterfaceEdge;
import br.edu.ufcg.splab.core.InterfaceGraph;
import br.edu.ufcg.splab.exceptions.LTSBTException;
import br.edu.ufcg.splab.util.ReadTGF;

public class CompareParsersTest {

	@Test
	public void testCompareGraphContraction() {		
		try {
			String path_graphs = "./test-data/graphToAnnotatedGraph/graphs/";
			String path_annotatedGraphs = "./test-data/graphToAnnotatedGraph/annotatedGraphs/";
			
			List<File> graphs = getFileListTGF(path_graphs);
			ReadTGF read = new ReadTGF();
			
			for(File file:graphs){
				InterfaceGraph graph = read.getGraph(path_graphs + file.getName());	
				
				CompareParsers parser = new CompareParsers();
				InterfaceGraph annotatedGraph = parser.compareGraphContraction(graph);
		
				InterfaceGraph expectedGraph = read.getGraph(path_annotatedGraphs + file.getName().substring(0,file.getName().length() - 4) + "_test.tgf");
				
				for(InterfaceEdge edge: expectedGraph.getEdges()){
					assertTrue(annotatedGraph.containsEdge(edge));
				}		
				assertTrue(expectedGraph.getEdges().size() == annotatedGraph.getEdges().size());
				assertTrue(expectedGraph.getStates().keySet().size() == annotatedGraph.getStates().keySet().size());
			}
		} catch (LTSBTException e){
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testGraphContraction2() {		
		try {
			String path_graph = "./test-data/graphToAnnotatedGraph/graphs2/graph1.tgf";
			ReadTGF read = new ReadTGF();			
			InterfaceGraph graph = read.getGraph(path_graph);	
				
			GraphToAnnotatedGraphParser parser = new GraphToAnnotatedGraphParser();
			parser.graphContraction(graph);
		
		} catch (LTSBTException e){
			assertTrue(e.getMessage().equals("Invalid graph: the inputs are different edge types for the vertex 8 (default and expected_results)."));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testGraphContraction3() {		
		try {
			String path_graph = "./test-data/graphToAnnotatedGraph/graphs2/graph2.tgf";
			ReadTGF read = new ReadTGF();			
			InterfaceGraph graph = read.getGraph(path_graph);	
				
			GraphToAnnotatedGraphParser parser = new GraphToAnnotatedGraphParser();
			parser.graphContraction(graph);
		
		} catch (LTSBTException e){
			assertTrue(e.getMessage().equals("Invalid graph: two edges followed of the default type (default, 12, default, 13)."));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testGraphContraction4() {		
		try {
			String path_graph = "./test-data/graphToAnnotatedGraph/graphs2/graph3.tgf";
			ReadTGF read = new ReadTGF();			
			InterfaceGraph graph = read.getGraph(path_graph);	
				
			GraphToAnnotatedGraphParser parser = new GraphToAnnotatedGraphParser();
			parser.graphContraction(graph);
		
		} catch (LTSBTException e){
			assertTrue(e.getMessage().equals("Invalid graph: two edges followed of different types (7, steps, 18, expected_results, 19)."));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
		
	private List<File> getFileListTGF(String filepath){
        File filePath = new File(filepath);
        List<File> fileList = new ArrayList<File>();
        File[] files = filePath.listFiles();
        List<File> fileandFolderList = Arrays.asList(files);
        for (File file : fileandFolderList) {
        	if(file.getName().substring(file.getName().length() - 4, file.getName().length()).equals(".tgf")){
        		fileList.add(file);
        	}
        }
        return fileList;
    }
	
}
